package cn.livingCloud.controller.user.validator;

import cn.livingCloud.dto.UserFormDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2015/12/3.
 */
@Component
public class UserFormDtoValidator implements Validator {

    public boolean supports(Class<?> aClass) {
        return UserFormDto.class.equals(aClass);
    }

    public void validate(Object target, Errors errors) {
        UserFormDto formDto = (UserFormDto) target;
        validateName(formDto, errors);
//        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", null, "名称不能为空");
    }

    private void validateName(UserFormDto formDto, Errors errors) {
        if (StringUtils.isEmpty(formDto.getName())) {
            errors.rejectValue("name", null, "名称不能为空");
        }
    }
}
